angular.module("crawlerApp").controller("singleTaskCtrl",function($scope,$http,$state,$stateParams,menuService,$rootScope){
    $rootScope.menus=menuService.getBasicMenu("basic").$$state.value.initMenu;
    $rootScope.data=$stateParams.config;

    $scope.updateData=$stateParams.config;
    detailData($stateParams.config);

    $scope.urlType=["simple","url_unchanged"];

    function detailData(data) {
       // $scope.sourceResult = data;
       // $scope.updateData = angular.copy(data);
        $http.get("crawler/config/find/" + data.id).then(function (response) {
            $scope.customResult = response.data;
        })
    };


    function oneTaskInfo(data){
        $http.get("crawler/target/find/" + data.id).then(function (response) {
            $scope.updateData = response.data;
        })
    }

    $scope.deleteParse = function (data) {
        $http.delete("crawler/config/deleteParse/" + data.id).then(function (response) {
            // $scope.sourceResult = response.data;
            if (response.status == '200') {
                alert("删除成功！");
            }
            //getConfigList($scope.type)
            detailData( $scope.updateData);
        })

    };
    $scope.cancel=function () {
        oneTaskInfo($scope.updateData);
    };

    $scope.confirm = function () {
        // todo 修改之后的结果

        $http.put("crawler/config/saveOrUpdate", $scope.updateData).then(function (response) {
          //  $scope.sourceResult = response.data;
            oneTaskInfo($scope.updateData);
            updateParse();
           // getConfigList($scope.type);
        });

    };

    function updateParse() {
        $http.put("crawler/config/updateParse", $scope.customResult).then(function (response) {
            // $scope.sourceResult = response.data;
            //getConfigList($scope.type)
            if (response.status == '200') {
                alert("修改成功！");
            }
            detailData( $scope.updateData);
        })
    }


    $scope.parse = function () {
         $http.post("crawler/preAddConfig", $scope.updateData).then(function (response) {
             getCssSelector($stateParams.config.id);
         })
    };

    /*$scope.newData = [{
        customSelect: '',
        selectDescribe: ''
    }];*/
 /*   $scope.parseData={
        customSelect: '',
        selectDescribe: '',
        ruleId:''
    };*/
    $scope.commitParse = function (data) {
      /*  $scope.parseData = angular.copy(data);
        $scope.parseData.ruleId = angular.copy($stateParams.config.id);*/
        $http.put("crawler/config/parseAdd", data).then(function (response) {
            //$scope.sourceResult = response.data;
           // getConfigList($scope.type)
            detailData($scope.updateData);
        });

    };
    getDataType();

    function getDataType() {
        $http.get("crawler/dataFormat/getDataType").then(function (response) {
            $scope.dataTypes = response.data;
        });

    };

  /*  getDataFormat($scope.updateData.dataType);

    function getDataFormat(type){
        $http.get("crawler/dataFormat/getFormat/"+type).then(function (response) {
            $scope.dataFormats=response.data;
        })
    }*/


  function getCssSelector(id){
      $http.get("crawler/getCssSelector/"+id).then(function (response) {
          $scope.dataFormats=response.data;
      });
  }
});